#include "GameObject.h"
#include "Ogre.h"

using namespace Ogre;

// Constructor
GameObject::GameObject() {
	// Empty for now..
}

// Destructor
GameObject::~GameObject() {
	// Empty for now..
}

// Set a mesh for this object
void GameObject::setModel( Ogre::String modelname, Ogre::String mesh ) {
	if( gmName == "" ) {
		gmName = modelname;
	}
	Ogre::Root *mRoot = Root::getSingletonPtr();
	Ogre::SceneManager *mSceneMgr = mRoot->getSceneManager( "ST_GENERIC" );
    try {
		// Create a new entity
		gmModel = mSceneMgr->createEntity( modelname, mesh );
	} catch( Ogre::Exception &e ) {
		Ogre::Log( e.getDescription() );
	}
	// Create node for this entity
	gmSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	// Attach entity to the new node
	gmSceneNode->attachObject(gmModel);
	gmModel->setUserObject( this );
	
}

// Destroy entity for this object
void GameObject::destroy() {
	// Detach entity from node
	gmSceneNode->detachObject(gmModel);
	// Destroy the entity
	mSceneMgr->destroyEntity(gmModel->getName());
	// Destroy the scene node
	mSceneMgr->destroySceneNode(gmSceneNode->getName());
}

// Move object to different position
void GameObject::moveTo(Ogre::Real x, Ogre::Real y, Ogre::Real z) {
	xPos = x;
	yPos = y;
	zPos = z;
	// Move object within the SceneNode
	gmSceneNode->translate(xPos, yPos, zPos);
}

// Change position with x, y and z coords
void GameObject::setPosition( Ogre::Real x, Ogre::Real y, Ogre::Real z ) {
	xPos = x;
	yPos = y;
	zPos = z;
	// Set new position
	gmSceneNode->setPosition(xPos, yPos, zPos);
}

// Scale object in 3 dimensions
void GameObject::setScale( Ogre::Real x, Ogre::Real y, Ogre::Real z ) {
	xScale = x;
	yScale = y;
	zScale = z;
	// Set new scale (1 = original size)
	gmSceneNode->scale(xScale, yScale, zScale);
}

// Scale object in X dimension
void GameObject::setScaleX( Ogre::Real x ) {
	xScale = x;
	// Change X scale, preserve Y and Z scale (1 = original size)
	gmSceneNode->scale(xScale, yScale, zScale);
}

// Scale object in Y dimension
void GameObject::setScaleY( Ogre::Real y ) {
	yScale = y;
	// Change Y scale, preserve X and Z scale (1 = original size)
	gmSceneNode->scale(xScale, yScale, zScale);
}

// Scale object in Z dimension
void GameObject::setScaleZ( Ogre::Real z ) {
	zScale = z;
	// Change Z scale, preserve X and Y scale (1 = original size)
	gmSceneNode->scale(xScale, yScale, zScale);
}

// Rotate in X-ash of the SceneNode
void GameObject::rotateX( Ogre::Degree dg ) {
	xRotate = dg;
	// Pitch SceneNode
	gmSceneNode->pitch(xRotate);
}

// Rotate in Y-ash of the SceneNode
void GameObject::rotateY( Ogre::Degree dg ) {
	yRotate = dg;
	// Yaw SceneNode
	gmSceneNode->yaw(yRotate);
}

// Rotate in Z-ash of the SceneNode
void GameObject::rotateZ( Ogre::Degree dg ) {
	zRotate = dg;
	// Roll SceneNode
	gmSceneNode->roll(zRotate);
}

void GameObject::importSceneNode( Ogre::Entity *entity, Ogre::SceneNode *sceneNode ) {
	gmModel = entity;
	this->gmSceneNode = sceneNode;
}